/*
 * Created on 2005-8-27
 *
 * TODO To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package com.store;

import com.jedi.BaseObj;
import com.jedi.KeyGen;
import com.tss.util.DbConn;
import com.tss.util.DbRs;

/**
 * @author dai
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class DownCate extends BaseObj {
	public DownCate() {}
	public DownCate(String id) {this.categoryId = id;}
	public void insert() {
		clearErr();
		DbConn conn = new DbConn();
		try {
			String sql = "";
			setId(KeyGen.nextID(""));
			if (!isLeaf()) {
				setParentId(getId());
			} else {
				sql = "select * from t_soft_category where category_id = ?";
				conn.prepare(sql);
				conn.setString(1,getParentId());
				DbRs rs = conn.executeQuery();
				if (rs == null || rs.size() == 0) {
					setErr("此分类已经存在！");
					return;
				}
			}
			sql = "insert into t_soft_category (" +
				"category_id,category_name,parent_id,order_id,is_leaf,category_desc" +
				") values ( ?,?,?,?,?,? )";
			conn.prepare(sql);
			conn.setString(1,getId());
			conn.setString(2,getCategoryName());
			conn.setString(3,getParentId());
			conn.setInt(4,getOrderId());
			conn.setInt(5,getIsLeafVal());
			conn.setString(6,getCategoryDesc());
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}
	/*public void insert() {
		clearErr();
		DbConn conn = new DbConn();
		try {
			String sql = "";
			setId(KeyGen.nextID(""));
			sql = "insert into t_soft_category (" +
				"category_id,category_name,order_id,category_desc" +
				") values ( ?,?,?,? )";
			conn.prepare(sql);
			conn.setString(1,getId());
			conn.setString(2,getCategoryName());
			conn.setInt(3,getOrderId());
			conn.setString(4,getCategoryDesc());
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}*/
	
	public void update() {
		clearErr();
		DbConn conn = new DbConn();
		try {
			if (getId().trim().equals("")) {
				setErr("û��Ҫ���µļ�¼ID��");
				return;
			}
			String sql = "update t_soft_category set" +
				" category_name = ?,order_id = ?,category_desc = ?" +
				" where category_id = ?";
			conn.prepare(sql);
			conn.setString(1,getCategoryName());
			conn.setInt(2,getOrderId());
			conn.setString(3,getCategoryDesc());
			conn.setString(4,getId());
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}
	
	public void delete() {
		clearErr();
		DbConn conn = new DbConn();
		DbRs rs = null;
		try {
			String sql = "";
			sql = "select * from t_download_info where category_id = ?";
			conn.prepare(sql);
			conn.setString(1,getId());
			rs = conn.executeQuery();
			if (rs != null && rs.size() > 0) {
				setErr("�˷��໹�������������������������Ϣ����ɾ��!");
				return;
			}
			
			sql = "delete from t_soft_category where category_id = ?";
			conn.prepare(sql);
			conn.setString(1,getId());
			conn.executeUpdate();
		}
		catch(Exception ex) {
			ex.printStackTrace();
			setErr(ex.getMessage());
		}
		finally {
			conn.close();
		}
	}
	
	public String getId() {return categoryId;}
	public String getCategoryName() {return categoryName;}
	public int getOrderId() {return orderId;}
	public String getParentId() {return parentId;}
	public String getCategoryDesc() {return categoryDesc;}
	public boolean isLeaf() {return (is_Leaf == 1)?true:false;}
	public int getIsLeafVal() {return is_Leaf;}
	public void setId(String categoryId) {this.categoryId = categoryId;}
	public void setCategoryName(String categoryName) {this.categoryName = categoryName;}
	public void setParentId(String parentId) {this.parentId =parentId;}
	public void setOrderId(int orderId) {this.orderId = orderId;}
	public void setCategoryDesc(String categoryDesc) {this.categoryDesc = categoryDesc;}
	public void setLeaf() {this.is_Leaf = 1;}
	private String categoryId = "";
	private String categoryName = "";
	private int orderId = 0;
	private int is_Leaf = 0;
	private String parentId = "";
	private String categoryDesc = "";
	public String getCategoryId() {
		return categoryId;
	}
	public void setCategoryId(String categoryId) {
		this.categoryId = categoryId;
	}
}
